package com.xtaller.lazy.api.system;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xtaller.lazy.base.TApi;
import com.xtaller.lazy.lib.annotation.Permission;
import com.xtaller.lazy.lib.convert.J;
import com.xtaller.lazy.model.system.DataLog;
import com.xtaller.lazy.model.system.ExLog;
import com.xtaller.lazy.model.system.LoginLog;
import com.xtaller.lazy.service.base.UserService;
import com.xtaller.lazy.service.system.DataLogService;
import com.xtaller.lazy.service.system.ExLogService;
import com.xtaller.lazy.service.system.LoginLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @version V1.0
 * @author: Taller
 * @date: 2019-04-23
 * @Description: 日志类api
 */
@Api(tags = "日志类api")
@RestController
@RequestMapping("/v1/log")
public class LogApi extends TApi {
    @Autowired private DataLogService dataLogService;
    @Autowired private ExLogService exLogService;
    @Autowired private LoginLogService loginLogService;


    @GetMapping("/data")
    @ApiOperation("读取数据日志")
    @Permission(authorities = "system-data-log-table")
    public Object dataTable(@ApiParam("页码") @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                            @ApiParam("页长") @RequestParam(value = "limit", required = false, defaultValue = "50") int limit,
                            @ApiParam("关键字") @RequestParam(value = "key", required = false, defaultValue = "") String key){
        var p = new Page<DataLog>(page, limit);
        var qw = new QueryWrapper<DataLog>();
        if(key.length() > 0){
            qw.lambda().or(x -> x.like(DataLog::getMethod, key)
                    .or().like(DataLog::getUrl, key)
                    .or().like(DataLog::getDataId, key)
                    .or().like(DataLog::getCreateId, key));
        }
        qw.lambda().orderByDesc(DataLog::getCreateTime);
        var result = p2t(dataLogService.page(p, qw));
        var data = J.o2l(result.get("data"));
        result.put("data", f2c(data));

        return result;
    }

    @GetMapping("/ex")
    @ApiOperation("读取异常数据日志")
    @Permission(authorities = "system-ex-log-table")
    public Object exTable(@ApiParam("页码") @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                          @ApiParam("页长") @RequestParam(value = "limit", required = false, defaultValue = "50") int limit,
                          @ApiParam("关键字") @RequestParam(value = "key", required = false, defaultValue = "") String key){
        var p = new Page<ExLog>(page, limit);
        var qw = new QueryWrapper<ExLog>();
        if(key.length() > 0){
            qw.lambda().or(x -> x.like(ExLog::getMethod, key)
                    .or().like(ExLog::getUrl, key)
                    .or().like(ExLog::getDataId, key)
                    .or().like(ExLog::getCreateId, key));
        }
        qw.lambda().orderByDesc(ExLog::getCreateTime);
        var result = p2t(exLogService.page(p, qw));
        var data = J.o2l(result.get("data"));

        result.put("data", f2c(data));
        return result;
    }

    @GetMapping("/login")
    @ApiOperation("读取登录日志")
    @Permission(authorities = "system-login-log-table")
    public Object loginTable(@ApiParam("页码") @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                             @ApiParam("页长") @RequestParam(value = "limit", required = false, defaultValue = "50") int limit,
                             @ApiParam("关键字") @RequestParam(value = "key", required = false, defaultValue = "") String key,
                             @ApiParam("所属平台") @RequestParam(value = "platform", required = false, defaultValue = "0") int platform){
        var p = new Page<LoginLog>(page, limit);
        var qw = new QueryWrapper<LoginLog>();
        qw.lambda().orderByDesc(LoginLog::getCreateTime);
        if(platform > 0){
            qw.lambda().eq(LoginLog::getPlatform, platform);
        }
        if(key.length() > 0){
            qw.lambda().or(x -> x.like(LoginLog::getLoginName, key)
                                .or().like(LoginLog::getCreateId, key));
        }
        return p2t(loginLogService.page(p, qw));
    }
}
